package com.ctos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctos.Exception.CustomException;
import com.ctos.entity.Category;
import com.ctos.entity.Dish;
import com.ctos.entity.Setmeal;
import com.ctos.mapper.CategoryMapper;
import com.ctos.service.CategoryService;
import com.ctos.service.DishService;
import com.ctos.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
   public void remove(Long id){
        //查询当前分类是否关联菜品，如果以及关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        Integer count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1>0){
            throw  new CustomException("该分类下还有菜品，无法删除");
        }

        //查询当前分类是否关联套餐，如果以及关联，抛出一个业务异常

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        Integer count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("该分类下还有套餐，无法删除");
        }

        //正常删除

        super.removeById(id);

    }

}
